package com.kt.common.collections

fun main() {
    /**创建序列的方法*/
    //直接通过sequenceOf创建
    //val numbersSequence = sequenceOf("four", "three", "two", "one")
    //val numbers = listOf("one", "two", "three", "four")
    //通过iterate的asSequenceOf方法
    //val numbersSequence = numbers.asSequence()

    //由函数
    /*val oddNumbers = generateSequence(6) { it + 2 } // `it` 是上一个元素
    println(oddNumbers.take(6).toList())*/
    //println(oddNumbers.count())     // 错误：此序列是无限的。

    //由组块
    /*val oddNumbers = sequence {
        yield(-9)
        yieldAll(listOf(-7,-4))
        yield(1)
        yieldAll(listOf(3, 5))
        yieldAll(generateSequence(7) { it + 2 })
    }
    println(oddNumbers.take(9).toList())*/

    //序列操作 ,使用take 与drop 管理
    val words = "The quick brown fox jumps over the lazy dog".split(" ")
    val lengthsList = words.filter { println("filter: $it"); it.length > 3 }
        .map { println("length: ${it.length}"); it.length }
        .take(4)

    println("Lengths of first 4 words longer than 3 chars:")
    println(lengthsList)
}